<template>
  <div class="app-container">
    <el-table border v-loading="loading" :data="lineList" @selection-change="handleSelectionChange">
      <el-table-column label="物料编码" align="center" prop="itemCode" width="180" :show-overflow-tooltip="true"/>
      <el-table-column label="物料名称" align="center" prop="itemName" width="150px" :show-overflow-tooltip="true"/>
      <el-table-column label="规格型号" align="center" prop="specification" width="120px" :show-overflow-tooltip="true"/>
      <el-table-column label="单位" align="center" prop="unitOfMeasure" width="80px" :show-overflow-tooltip="true"/>
      <el-table-column
        v-for="col in columnsConfig"
        :key="col.prop"
        :label="col.label"
        :prop="col.prop"
        :width="col.width"
        :align="col.align"
        v-if="col.show">
      </el-table-column>
      <el-table-column label="消耗数量" align="center" prop="quantityConsume" width="120px" :show-overflow-tooltip="true"/>
      <el-table-column label="领料批次号" align="center" prop="batchCode" width="200px" :show-overflow-tooltip="true"/>
      <!--      <el-table-column label="仓库名称" align="center" prop="warehouseName" width="200px" :show-overflow-tooltip="true"/>
            <el-table-column label="库区名称" align="center" prop="locationName" width="200px" :show-overflow-tooltip="true"/>
            <el-table-column label="库位名称" align="center" prop="areaName" width="200px" :show-overflow-tooltip="true"/>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {listLine} from "@/api//mes/wm/consumeline";
import {getColumnsConfigKey} from "@/api/system/columnsconfig";

export default {
  name: "consumeLine",
  props: {
    recordId: null,
  },
  data() {
    return {
      columnsConfig: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料消耗记录行表格数据
      lineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        recordId: this.recordId,
        materialStockId: null,
        itemId: null,
        itemCode: null,
        itemName: null,
        specification: null,
        unitOfMeasure: null,
        quantityConsume: null,
        batchCode: null,
        warehouseId: null,
        warehouseCode: null,
        warehouseName: null,
        locationId: null,
        locationCode: null,
        locationName: null,
        areaId: null,
        areaCode: null,
        areaName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        itemId: [
          {required: true, message: "产品物料ID不能为空", trigger: "blur"}
        ], quantityConsume: [
          {required: true, message: "消耗数量不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getItemBase();
    this.getList();
  },
  methods: {
    /** 查询物料消耗记录行列表 */
    getList() {
      this.loading = true;
      listLine(this.queryParams).then(response => {
        this.lineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        lineId: null,
        recordId: this.recordId,
        materialStockId: null,
        itemId: null,
        itemCode: null,
        itemName: null,
        specification: null,
        unitOfMeasure: null,
        quantityConsume: null,
        batchCode: null,
        warehouseId: null,
        warehouseCode: null,
        warehouseName: null,
        locationId: null,
        locationCode: null,
        locationName: null,
        areaId: null,
        areaCode: null,
        areaName: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.lineId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料消耗记录行";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const lineId = row.lineId || this.ids
      getLine(lineId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物料消耗记录行";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs[ "form" ].validate(valid => {
        if (valid) {
          if (this.form.lineId != null) {
            updateLine(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLine(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const lineIds = row.lineId || this.ids;
      this.$modal.confirm('是否确认删除物料消耗记录行编号为"' + lineIds + '"的数据项？').then(function () {
        return delLine(lineIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('md/line/export', {
        ...this.queryParams
      }, `line_${new Date().getTime()}.xlsx`)
    },
    getItemBase() {
      getColumnsConfigKey('MdItem_Base').then(res => {
        this.columnsConfig = JSON.parse(res.data);
      })
    },
  }
};
</script>
